<template>
  <div class="item" style="width: 100%">
    <div class="nameItem">
      <i v-if="itemData.topTree" class="el-icon-s-check iconClass" />
      <i v-else class="el-icon-s-custom iconClass" />
      {{ itemData.name }}
    </div>
    <div class="name">{{ itemData.manager }}</div>
    <el-dropdown @command="commandEvent">
      <div class="namItem2">
        <span>操作</span>
        <i class="el-icon-arrow-down" />
      </div>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item command="add">添加子集部门</el-dropdown-item>
          <el-dropdown-item
            v-if="!itemData.topTree"
            command="edit"
          >查看部门</el-dropdown-item>
          <el-dropdown-item
            v-if="!itemData.topTree"
            command="del"
          >删除部门</el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>

<script>
import { companyDepartmentDelte } from '@/api/departments.js'
export default {
  props: {
    itemData: {
      type: Object,
      default: () => ({})
    }
  },
  methods: {
    commandEvent(command) {
      switch (command) {
        case 'add':
          this.$bus.$emit('itemShow', true, this.itemData, command)
          console.log(this.itemData)
          break
        case 'del':
          this.$confirm('确认删除该部门吗？', '提示', {
            type: 'warning'
          })
            .then(async() => {
              await companyDepartmentDelte(this.itemData.id)
              this.$message.success('删除成功')
              this.$emit('getData')
            })
            .catch()
          break
        case 'edit':
          this.$bus.$emit('itemShow', true, this.itemData, command)
          break
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.item {
  display: flex;
  .nameItem {
    flex: 1;
    .iconClass {
      margin: 0 5px;
    }
  }
  .name {
    margin: 0 15px;
    color: #657d8f;
    font-size: 14px;
  }
  .nameItem2 {
    .el-icon-arrow-down {
      padding: 0 5px;
    }
  }
}
</style>
